package com.example.fitnessservice.module.dao.coachmapper;

import com.example.fitnessservice.module.pojo.*;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/21 11:05
 */
@Mapper
public interface CoachMapper {
    /***
     * 查询所有教练
     * @return
     */
    @Select("select * from coach")
    public List<Coach> findAll();

    /***
     * 根据教练id查询钱包余额
     * @param cid
     * @return
     */
    @Select("select balance from coach where id=#{cid}")
    public Integer findWalletByCid(Integer cid);

    /***
     * 通过多表查询，查询教练对应课程
     * @param cid
     * @return
     */
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "directorId",column = "director_id"),
            @Result(property = "name",column = "name"),
            @Result(property = "age",column = "age"),
            @Result(property = "birthday",column = "birthday"),
            @Result(property = "phone",column = "phone"),
            @Result(property = "qq",column = "qq"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "level",column = "level"),
            @Result(property = "email",column = "email"),
            @Result(property = "fans",column = "fans"),
            @Result(property = "balance",column = "balance"),
            @Result(property = "courseDetails", column = "id", many = @Many(select = "com.example.fitnessservice.module.dao.courseMapper.CoachCourseMapper.findByCid"))
    })
    @Select("select * from coach where id=#{cid}")
    public Coach findClassByCid(Integer cid);

    /***
     * 通过cid查询教练订单
     * @param cid
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderTime", column = "order_time"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "orderCourseId", column = "order_course_id"),
            @Result(property = "orderCoachId", column = "order_coach_id"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "orderMoney", column = "order_money"),
            @Result(property = "courseDetails", column = "order_course_id", javaType = CourseDetails.class, one = @One(select = "com.example.fitnessservice.module.dao.courseMapper.CoachCourseMapper.findTimeByCid"))
    })
    @Select("select * from course_order_details where order_coach_id=#{cid}")
    public List<CourseOrderDetails> findOrderByCid(Integer cid);

    /***
     * 将教练订单状态改为1表已完成
     * @param id
     */
    @Update("update course_order_details set order_status=1 where id=#{id}")
    public void updateorderStatus(int id);

    //每天改一改数据库和这里
    /***
     * 用于传入时间进行对比教练在该时间段是否有课
     * @return
     */
    @Select("select a.*,b.id1 from coach a left join (select course_chief_coach_id as id1 from course_details where course_start_time='2022-11-25 10:00:00.000' and course_end_time='2022-11-25 11:00:00.000') b on a.id=b.id1")
    public List<Coach> findAll1();

    @Select("select a.*,b.id1 from coach a left join (select course_chief_coach_id as id1 from course_details where course_start_time='2022-11-25 15:00:00.000' and course_end_time='2022-11-25 17:00:00.000') b on a.id=b.id1")
    public List<Coach> findAll2();

    @Select("select a.*,b.id1 from coach a left join (select course_chief_coach_id as id1 from course_details where course_start_time='2022-11-25 19:00:00.000' and course_end_time='2022-11-25 20:30:00.000') b on a.id=b.id1")
    public List<Coach> findAll3();

    /***
     * 查看所有课程详情
     * @return
     */
    @Select("select * from course_details")
    public List<CourseDetails> findCourseDetails();

    /***
     * 查看教练订单详情通过教练id
     * @param cid
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderTime", column = "order_time"),
            @Result(property = "orderNum", column = "order_num"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "orderCourseId", column = "order_course_id"),
            @Result(property = "orderCoachId", column = "order_coach_id"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "orderMoney", column = "order_money"),
            @Result(property = "courseDetails", column = "order_course_id", javaType = CourseDetails.class, one = @One(select = "com.example.fitnessservice.module.dao.courseMapper.CoachCourseMapper.findCourseByCid"))
    })
    @Select("select * from course_order_details where order_coach_id=#{cid}")
    public List<CourseOrderDetails> findCourseOrderDetails(Integer cid);

    /***
     * 通过学员id查询学员信息
     * @param uid
     * @return
     */
    @Select("select * from user_info where id=#{uid}")
    public UserInfo findUserById(Integer uid);

    /***
     * 向用户预约表中添加数据
     */
    @Insert("insert into coach_booking (id,coach_id,coach_name,user_id,username,order_time,order_status,course_id) value (null,#{cid},#{cname},#{uid},#{uname},#{ordertime},#{orderstatus},#{coid})")
    public void addCoachBooking(Integer cid, String cname, Integer uid, String uname, String ordertime, Integer orderstatus, Integer coid);

    @Select("select name from coach where id=#{cid}")
    public String findNameByCid(Integer Cid);

    /***
     * 这个主要针对结合courch_order_details
     * @param cid
     * @return
     */
    @Select("select * from coach_booking where coach_id=#{cid}")
    public List<CoachBooking> findCoachBookingByCid(Integer cid);

    @Update("update course_details set course_status=1 where id=#{id}")
    public void updateCourse_statusById(Integer id);

    @Select("select * from course_details where id=#{id}")
    public CourseDetails findByCourseId(Integer id);

    @Update("update coach_booking set order_status=1 where id=#{id}")
    public void updateBookingStatus(Integer id);
    @Select("select * from coach where id=#{id}")
    Coach findCoachById(Integer id);
}
